import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2023-02-24
 * Time: 9:15
 */
class Student{
    String id;

    public Student(String id) {
        this.id = id;
    }

    /**
     * equals与hashcode重写后,都是按照引用对象里的具体成员来比较
     * @param o
     * @return
     */
//    //equals用于循环遍历链表时判断key
//    //也是根据对象里的属性来判断是否相等
//    @Override
//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//        Student student = (Student) o;
//        return Objects.equals(id, student.id);
//    }
//    //根据对象的具体属性计算hashcode
////现实生活中认为:两个的主码一样,那么就是同一个人例如身份证号一样,
//    @Override
//    public int hashCode() {
//        //重写object类的hashcode,根据id来计算hash
//        return Objects.hash(id);
//    }
}

public class GenericityHashBuckTest {
    public static void main(String[] args) {
        GenericityHashBuck genericityHashBuck = new GenericityHashBuck();
//        Student student1 = new Student("123");
//        Student student2 = new Student("234");
//        Student student3 = new Student("345");
//        Student student4 = new Student("456");
//        Student student5 = new Student("567");
//        Student student6 = new Student("786");
//        Student student7 = new Student("235");
//        Student student8 = new Student("157");
//        Student student9 = new Student("575");
//        Student student10 = new Student("133");
//        Student student11 = new Student("163");
//        Student student12 = new Student("103");
//
//        genericityHashBuck.put(student1,"denghui");
//        genericityHashBuck.put(student2,"denghui");
//        genericityHashBuck.put(student3,"denghui");
//        genericityHashBuck.put(student4,"denghui");
//        genericityHashBuck.put(student5,"denghui");
//        genericityHashBuck.put(student6,"denghui");
//        genericityHashBuck.put(student7,"denghui");
//        genericityHashBuck.put(student8,"denghui");
//        genericityHashBuck.put(student9,"denghui");
//        genericityHashBuck.put(student10,"denghui");
//        genericityHashBuck.put(student11,"denghui");
//        genericityHashBuck.put(student12,"denghui");
//        System.out.println(genericityHashBuck.get(student2));

        genericityHashBuck.put(1,1);
        genericityHashBuck.put(2,2);
        genericityHashBuck.put(3,3);
        genericityHashBuck.put(4,4);
        genericityHashBuck.put(14,14);
        genericityHashBuck.put(5,5);
        genericityHashBuck.put(6,6);
        genericityHashBuck.put(7,7);
        genericityHashBuck.put(8,8);
        genericityHashBuck.put(9,9);
        genericityHashBuck.put(10,10);
        genericityHashBuck.put(11,11);

    }

    public static void main1(String[] args) {
        HashBuck2 map = new HashBuck2();
        //根据对象里的id来计算hash,这两个对象的hash值应该一样才是我们期望的结果,因此重写hashcode
        //object类的hashcode是直接计算对象引用的hash值(根据地址):
        //在合理实用的情况下，类Object定义的hashCode方法确实为不同的对象返回不同的整数。(这通常是通过将对象的内部地址转换为整数来实现的，但是Java™编程语言并不需要这种实现技术。)
        Student student1 = new Student("123");
        Student student2 =new Student("123");
        map.put(student1,"gaobo");
        //这里key是student类型,而student1和student2的id都一样,那么认为这两对象就是同一个对象,因为student1.equals(student2)相等
        //hashcode根据id来生成,equals根据id来比较,equals为true那么hashcode一定一样
        System.out.println(map.get(student2));
    }
}
